﻿<%@ WebHandler Language="C#" Class="HandlerGoogleMap" %>

using System;
using System.Web;
using Newtonsoft.Json;
using System.Data.SqlClient;
using System.Data;
public class HandlerGoogleMap : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        string strconn = LinkToDatabase.strConn;/*App_Code->UserClass.cs*/
        string strcmd = "";
        int UserId = 0;
        if (context.Request.Cookies["u_id"] != null && context.Request.Cookies["u_id"].Value != "null"){
            UserId = Convert.ToInt32(context.Request.Cookies["u_id"].Value);
        }
        string type = context.Request.Form["type"];
        using (SqlConnection conn = new SqlConnection(strconn))
        {
            switch (type)
            {
                //1=======================================================================
                case "category":
                    strcmd = @"
                              SELECT [category_id] as value
                              ,[category_name] as category
                              FROM [UBOX].[dbo].[category]
                            ";
                    break;
                //2=======================================================================
                case "brand":
                    strcmd = @"
                             SELECT [brand_id] as value
                             ,[brand_name] as brand
                             FROM [UBOX].[dbo].[brand]
                            ";
                    break;
                //3=======================================================================
                case "product":
                    strcmd = @"
                              SELECT [product_id] as value
                              ,[product_name] as product
                              FROM [UBOX].[dbo].[production_product]
                              where [category_id] =@CId and [brand_id]=@BId and [product_visible]=1
                            ";
                    break;
                //4=======================================================================
                case "Gm":
                    strcmd = @"
                                INSERT INTO [dbo].[google_map] (
                                [dbo].[user_id],
                                [product_id],
                                [product_price],
                                [geo_info],
                                [geo_address_x],
                                [geo_address_y],
                                [date]) 
                                VALUES (@UId,@Product,@Price,@Info,@X,@Y,CONVERT(NVARCHAR, GETDATE(), 112))
                                select @@rowcount as Result
                            ";
                    break;
                //5=======================================================================
                case "MarkerRead":
                    strcmd = @"
                               SELECT 
                               [product_price] as price
                               ,[geo_info] as information
                               ,[geo_address_x] as x
                               ,[geo_address_y] as y
                               FROM [UBOX].[dbo].[google_map]
                               where [date]>=CONVERT(NVARCHAR, GETDATE()- @Date, 112) 
                               and [geo_address_x]<@MaxX 
                               and [geo_address_x]>@MinX
                               and [geo_address_y]<@MaxY 
                               and [geo_address_y]>@MinY
                               and [product_id]=@PId
                               and [product_price]>@MinPrice 
                            ";
                    if (Convert.ToInt32(context.Request.Form["MaxPrice"]) != -1)
                    {
                        strcmd += "and [product_price]<@MaxPrice";
                    }strcmd += " order by [product_price] Desc";
                    break;
                        
            }
            using (SqlDataAdapter da = new SqlDataAdapter(strcmd, conn))
            {
                switch (type)
                {
                    //3=======================================================================
                    case "product":
                        //input
                        da.SelectCommand.Parameters.AddWithValue("@CId", context.Request.Form["category"]);
                        da.SelectCommand.Parameters.AddWithValue("@BId", context.Request.Form["brand"]);
                        break;
                    //4=======================================================================
                    case "Gm":
                        //input
                        da.SelectCommand.Parameters.AddWithValue("@X", context.Request.Form["x"]);
                        da.SelectCommand.Parameters.AddWithValue("@Y", context.Request.Form["y"]);
                        da.SelectCommand.Parameters.AddWithValue("@Product", context.Request.Form["product"]);
                        da.SelectCommand.Parameters.AddWithValue("@Price", context.Request.Form["price"]);
                        da.SelectCommand.Parameters.AddWithValue("@Info", context.Request.Form["info"]);
                        da.SelectCommand.Parameters.AddWithValue("@UId", UserId);
                    break;
                    //5=======================================================================
                    case "MarkerRead":
                        //input
                    da.SelectCommand.Parameters.AddWithValue("@Date",  Convert.ToInt32(context.Request.Form["Date"]));
                        da.SelectCommand.Parameters.AddWithValue("@MaxX", context.Request.Form["MaxX"]);
                        da.SelectCommand.Parameters.AddWithValue("@MinX", context.Request.Form["MinX"]);
                        da.SelectCommand.Parameters.AddWithValue("@MaxY", context.Request.Form["MaxY"]);
                        da.SelectCommand.Parameters.AddWithValue("@MinY", context.Request.Form["MinY"]);
                        da.SelectCommand.Parameters.AddWithValue("@PId", context.Request.Form["p"]);
                        da.SelectCommand.Parameters.AddWithValue("@MinPrice", context.Request.Form["MinPrice"]);
                        if (Convert.ToInt32(context.Request.Form["MaxPrice"]) != -1)
                        {
                            da.SelectCommand.Parameters.AddWithValue("@MaxPrice", context.Request.Form["MaxPrice"]);
                        }
                         break;   
                }
                DataSet ds = new DataSet();
                switch (type)
                {
                    case "category":
                    case "brand":
                    case "product":
                    case "Gm":
                    case "MarkerRead":
                        da.Fill(ds);
                        context.Response.ContentType = "application/json";
                        context.Response.Write(JsonConvert.SerializeObject(ds));
                        break;
                }
                conn.Close();
            }
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}